package com.cn.por.auto.factory;

import java.util.ArrayList;
import java.util.List;

import com.cn.por.auto.dto.DataModel;
import com.cn.por.auto.dto.FunctionMessage;
import com.cn.por.auto.dto.InterfaceDataModel;
import com.cn.por.auto.dto.ModelParam;
import com.cn.por.auto.entity.TableDetiles;
import com.cn.por.auto.enums.FunctionName;
import com.cn.por.util.freemarker.StringFormaterUtil;

/**
 * 接口数据模型工厂类，用于组装接口数据模型类。
 * @author wen
 *
 */
public class InterfaceDataModelFactory implements DataModelFactory {

	public InterfaceDataModelFactory() {}
	protected InterfaceDataModel interfaceDataModel;
	
	@Override
	public void setProperties(ModelParam modelParam) {
		interfaceDataModel= new InterfaceDataModel();
		/**
		 * entityName
		 */
		String entityName = StringFormaterUtil.columnToProperty(modelParam.getTableName());
		interfaceDataModel.setEntityName(entityName);
		/**
		 * 包名
		 */
		interfaceDataModel.setPackageName(
				modelParam.getBasePackage()
						+ "." + modelParam.getSuffixName().toLowerCase()
						);
		/**
		 * 类名称
		 */
		String suffix  = modelParam.getSuffixName();
		interfaceDataModel.setClassName(entityName + suffix.replace(".", ""));
		
		List<FunctionMessage> functionMsgs = new ArrayList<FunctionMessage>();
		/**
		 * 根据id获取
		 */
		FunctionMessage functionMessage = new FunctionMessage();
		functionMessage.setFunctionName(FunctionName.getById.name());
		functionMessage.setResultType(entityName);
		String primaryKeyType = "java.lang.String";
		String primaryKeyTypeSimple = "String";
		for (TableDetiles tc : modelParam.getTableColumn()) {
			if (tc.getColumnName().equals(modelParam.getPrimaryKey())) {
				if ("INT".equals(tc.getColumnType())) {
					primaryKeyType = "java.lang.Integer";
					primaryKeyTypeSimple = "Integer";
				}
			}
		}
		functionMessage.setParamterType(primaryKeyTypeSimple);
		functionMessage.setParamterName(modelParam.getPrimaryKey());
		functionMsgs.add(functionMessage);
		/**
		 * 动态条件获取集合
		 */
		FunctionMessage f2 = new FunctionMessage();
		f2.setFunctionName(FunctionName.getList.name());
		f2.setParamterType(entityName);
		f2.setResultType("List<"+entityName+">");
		f2.setParamterName(StringFormaterUtil.lowerCaptureName(entityName));
		functionMsgs.add(f2);
		/**
		 * 根据id更新
		 */
		FunctionMessage f3 = new FunctionMessage();
		f3.setFunctionName(FunctionName.updateById.name());
		f3.setParamterType(primaryKeyTypeSimple);
		f3.setParamterName(modelParam.getPrimaryKey());
		f3.setResultType("Integer");
		functionMsgs.add(f3);
		/**
		 * 新增
		 */
		FunctionMessage f4 = new FunctionMessage();
		f4.setFunctionName(FunctionName.addOne.name());
		f4.setParamterType(entityName);
		f4.setResultType("Integer");
		f4.setParamterName(StringFormaterUtil.lowerCaptureName(entityName));
		functionMsgs.add(f4);
		interfaceDataModel.setFunctionMsgs(functionMsgs);
		
		/**
		 * 导入的包
		 */
		List<String> importPackage  = new ArrayList<String>();
		importPackage.add("java.util.List");
		importPackage.add(modelParam.getBasePackage()+".entity." + entityName);
		interfaceDataModel.setImportPackage(importPackage);
		
		interfaceDataModel.setFileName(modelParam.getFileName());
		interfaceDataModel.setFtlName(modelParam.getFtlName());
		interfaceDataModel.setFilePath(modelParam.getFilePath());
		interfaceDataModel.setFolder(interfaceDataModel.getPackageName().replace(".", "/"));
	}

	@Override
	public DataModel getDataModel() {
		return this.interfaceDataModel;
	}

}
